--[[/**
-- * 
 * 护盾
-- */]]

---@class BuffEffect604100 : BaseBuffEffect
BuffEffect604100 = ClientFight.CreateClass("BuffEffect604100", ClientFight.BaseBuffEffect)
local t = BuffEffect604100
function t:add(fight, buff)
    local source = buff.source;
    local target = buff.target;
    local addValue = 0;
    local configDis = Utils.toRatio(buff.buffBean.f_BuffEffectValue[1]);
    local attrType = buff.buffBean.f_BuffEffectValue[2];
    local attrRatio = buff.buffBean.f_BuffEffectValue[3];
    local fightTeam = fight.teams[3 - buff.source.teamIndex];
    for k, _fighter in pairs(fightTeam.fighters) do
        if (_fighter == source) then
            goto continue;
        end
        if (_fighter:isDie()) then
            goto continue;
        end
        if (_fighter:isSummons()) then
            goto continue;
        end
        if (DistanceUtil.countDistance(buff.source.position, _fighter.position) > configDis) then
            goto continue;
        end
        local value = buff.overlay * (FighterAttributeManager.getFightAttr(_fighter, attrType) * toRatio(attrRatio));
        addValue = addValue + value;
        SkillRoleMgr.savePathFile("[" .. fight.frame .. "][EFFECT]Fighter " .. target.fighterId ..
                " Buff" .. buff.modelId .. " 增加护盾BUFF效果，护盾值：" .. value .. "  被吸收者:" .. _fighter.fighterId);
        :: continue ::
    end
    fightTeam = fight.teams[buff.source.teamIndex];
    for k, _fighter in pairs(fightTeam.fighters) do
        local value = buff.overlay * (FighterAttributeManager.getFightAttr(_fighter, attrType) * toRatio(attrRatio));
        if (_fighter == source) then
            --                continue;
        end
        if (_fighter:isDie()) then
            goto continue;
        end
        if (_fighter:isSummons()) then
            goto continue;
        end
        if (DistanceUtil.countDistance(buff.source.position, _fighter.position) > configDis) then
            goto continue;
        end
        addValue = addValue + value;
        SkillRoleMgr.savePathFile("[" .. fight.frame .. "][EFFECT]Fighter " .. target.fighterId ..
                " Buff" .. buff.modelId .. " 增加护盾BUFF效果，护盾值：" .. value .. "  被吸收者:" .. _fighter.fighterId);
        :: continue ::

    end
    if (addValue <= 0) then
        return IBuffEffect.FAIL;
    end

    local attrTypeLimit = buff.buffBean.f_BuffEffectValue[4];
    local attrRatioLimit = buff.buffBean.f_BuffEffectValue[5];
    local valueLimit = (long)(FighterAttributeManager.getFightAttr(source, attrTypeLimit) * toRatio(attrRatioLimit));

    if (addValue > valueLimit) then
        addValue = valueLimit;
    end
    buff.pool:setType(BuffPoolEnum.DEFEAT);
    addValue = buff.pool:setCurrent(addValue, buff);
    buff.pool:setMax(addValue);
    local logv = addValue;
    SkillRoleMgr.savePathFile("[" .. fight.frame .. "][EFFECT]Fighter " .. target.fighterId ..
            " Buff" .. buff.modelId .. " 增加护盾BUFF效果，总护盾值：" .. logv);

    return ClientFight.BaseBuffEffect.add(self, fight, buff);
end

---@param fight Fight
---@param buff FighterBuff
function t:action(fight, buff, ...)
    local parameters = { ... };
    local trigger = parameters[1];
    local result = parameters[3];

    local skill = parameters[2];
    local skillBean = skill.skill.skillBean;
    if (skillBean.f_HurtMethod == SkillDamageTypeEnum.HEAL) then
        return IBuffEffect.FAIL;
    end
    if (result.damage <= 0) then
        return IBuffEffect.FAIL;
    end

    SkillRoleMgr.savePathFile("[" .. fight.frame .. "][EFFECT]Fighter " .. buff.target.fighterId .. " Buff"
            .. buff.modelId .. " 触发护盾BUFF效果，护盾值：" .. buff.pool.current);
    local curr = buff.pool.current;
    if (result.damage >= buff.pool.current) then
        result.damage = result.damage - buff.pool.current;
        buff.pool:setCurrent(0, buff);
        if (result.damage == 0) then
            result.state = DamageResultTypeEnum.SHIELD;
        end
    else
        buff.pool:setCurrent(buff.pool.current - result.damage, buff);
        result.damage = 0;
        result.state = DamageResultTypeEnum.SHIELD;
        local commandInfo = ReportManager.buffToJsonString(fight, buff, 3)
        table.insert(fight.actions, commandInfo);
        FightManager.playActions(commandInfo)
    end

    SkillRoleMgr.savePathFile(
            "[" .. fight.frame .. "][DAMAGE]" .. buff.source.fighterId .. "对"
                    .. buff.target.fighterId .. "计算BUFF(" .. buff.modelId .. ")减伤伤害[4]结果,减免后伤害: "
                    .. NumberFormatUtil.formatDamage(result.damage) .. "  本次护盾抵消:" .. (curr - buff.pool.current));

    if (buff.pool.current <= 0) then
        return IBuffEffect.REMOVE;
    end
    return ClientFight.BaseBuffEffect.action(self, fight, buff, ...);
end

t.New()